home *** CD-ROM | disk | FTP | other *** search
- unit Base64;
-
- interface
- uses
- Windows, Messages, SysUtils, Classes;
-
- function encode(infilename: String; outfilename: String): boolean;
- function decode(infilename: String; outfilename: String): boolean;
-
- implementation
-
- function encode(infilename: String; outfilename: String): boolean;
- var
- encodedString: String;
- setof3ASCII: Array[1..3] of byte;
- counter: Integer;
- ord1,ord2,ord3: Integer;
- file2enc: TFileStream;
- read57: Array[1..57] of byte;
- strlength: LongInt;
- loops: Integer;
- encodedfile: TStringList;
- Base64Alphabet: String;
- begin
- Base64Alphabet:='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';;
- file2enc:=TFileStream.Create(infilename,fmOpenRead);
- encodedfile:=TStringList.Create;
- strlength:=file2enc.Read(read57,57);
- while strlength > 0 do
- begin // beginning of main loop
- encodedString:='';
-
- loops:=strlength div 3;
- for counter:=0 to loops -1 do
- begin
- setof3ASCII[1]:=read57[counter*3 +1];
- setof3ASCII[2]:=read57[counter*3 +2];
- setof3ASCII[3]:=read57[counter*3 +3];
- ord1:=ord(setof3ASCII[1]);
- ord2:=ord(setof3ASCII[2]);
- ord3:=ord(setof3ASCII[3]);
- encodedString:=encodedString+base64Alphabet[(ord1 div 4)+1];
- encodedString:=encodedString+base64Alphabet[(ord1 mod 4)*16 +(ord2 div 16)+1];
- encodedString:=encodedString+base64Alphabet[(ord2 mod 16)*4 +(ord3 div 64)+1];
- encodedString:=encodedString+base64Alphabet[ord3 mod 64+1];
- end;
- // two characters left over at end
- if( strlength mod 3 = 2) then
- begin
- setof3ASCII[1]:=read57[strlength-1];
- setof3ASCII[2]:=read57[strlength];
- ord1:=ord(setof3ASCII[1]);
- ord2:=ord(setof3ASCII[2]);
- encodedString:=encodedString+base64Alphabet[(ord1 div 4)+1];
- encodedString:=encodedString+base64Alphabet[(ord1 mod 4)*16 +(ord2 div 16)+1];
- encodedString:=encodedString+base64Alphabet[(ord2 mod 16)*4 +1];
- encodedString:=encodedString+'=';
- end;
- // one character left over at end
- if( strlength mod 3 = 1) then
- begin
- setof3ASCII[1]:=read57[strlength];
- ord1:=ord(setof3ASCII[1]);
- encodedString:=encodedString+base64Alphabet[(ord1 div 4)+1];
- encodedString:=encodedString+base64Alphabet[(ord1 mod 4)*16 +1];
- encodedString:=encodedString+'=';
- encodedString:=encodedString+'=';
- end;
-
- encodedfile.Add(encodedString);
- strlength:=file2enc.Read(read57,57);
- end; // end of main loop
- encodedfile.SaveToFile(outfilename);
- file2enc.Free;
- Result:=true;
- end;
-
- function decode(infilename: String; outfilename: String): boolean;
- var
- stringToDecode: String;
- setof4ASCII: Array[1..4] of char;
- setof57: Array[1..57] of char;
- maincounter, counter: Integer;
- ord1,ord2,ord3, ord4: Integer;
- use3rd, use4th: boolean;
- outf: TFileStream;
- total: Integer;
- loops: Integer;
- encodedfile: TStringList;
- Base64Alphabet: String;
- begin
- Base64Alphabet:='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';;
- encodedfile:=TStringList.Create;
- encodedfile.LoadFromFile(infilename);
- outf:=TFileStream.Create(outfilename,fmCreate);
-
- for maincounter:=0 to encodedfile.count - 1 do
- begin // beginning of main loop
- stringToDecode:=encodedfile.Strings[maincounter];
- total:=0;
- loops:=strlen(PChar(stringToDecode)) div 4;
-
- for counter:=0 to loops -1 do
- begin
- use3rd:=true;
- use4th:=true;
-
- setof4ASCII[1]:=stringTodecode[counter*4 +1];
- setof4ASCII[2]:=stringTodecode[counter*4 +2];
- setof4ASCII[3]:=stringTodecode[counter*4 +3];
- setof4ASCII[4]:=stringTodecode[counter*4 +4];
-
- ord1:=pos(setof4ASCII[1],base64Alphabet)-1;
- ord2:=pos(setof4ASCII[2],base64Alphabet)-1;
- if(setof4ASCII[3]='=') then
- begin
- ord3:=0;
- use3rd:=false;
- end
- else
- ord3:=pos(setof4ASCII[3],base64Alphabet)-1;
- if(setof4ASCII[4]='=') then
- begin
- ord4:=0;
- use4th:=false;
- end
- else
- ord4:=pos(setof4ASCII[4],base64Alphabet)-1;
-
- setof57[counter*3+1] :=chr( ord1*4+(ord2 div 16) );
- if use3rd then
- begin
- setof57[counter*3+2] :=chr( (ord2 mod 16)*16+(ord3 div 4) );
- inc(total);
- end;
- if use4th then
- begin
- setof57[counter*3+3] :=chr( (ord3 mod 4)*64 +ord4 );
- inc(total);
- end;
- inc(total);
- end;
- if total > 0 then
- for counter:=1 to total do
- outf.Write(setof57[counter],1);
- end; // end of main loop
- outf.Free;
- Result:=true;
- end;
-
- end.
-